package com.twetec.cms.service.website.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.twetec.cms.common.util.validate.ValidateUtil;
import com.twetec.cms.dao.mapper.ucenter.UserMapper;
import com.twetec.cms.dao.mapper.website.SiteColumnMapper;
import com.twetec.cms.dao.mapper.website.SiteMapper;
import com.twetec.cms.model.po.user.UserPO;
import com.twetec.cms.model.po.website.ColumnPO;
import com.twetec.cms.dao.mapper.website.ColumnMapper;
import com.twetec.cms.model.po.website.SiteColumnPO;
import com.twetec.cms.model.po.website.SitePO;
import com.twetec.cms.model.vo.website.ColumnPageListVO;
import com.twetec.cms.model.vo.website.ColumnVO;
import com.twetec.cms.model.vo.website.SiteColumnTempVO;
import com.twetec.cms.service.website.ColumnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twetec.cms.service.website.SiteColumnService;
import net.sf.jsqlparser.schema.Column;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhxl
 * @since 2021-09-06
 */
@Service
public class ColumnServiceImpl extends ServiceImpl<ColumnMapper, ColumnPO> implements ColumnService {

    private static final Logger logger = LoggerFactory.getLogger(SiteServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SiteMapper siteMapper;

    @Autowired
    private SiteColumnMapper siteColumnMapper;

    @Autowired
    private ColumnMapper columnMapper;

    /**
     * 校验栏目编码
     *
     * @param siteId
     * @param parentId
     * @param columnId
     * @param columnEncoded
     * @return
     */
    @Override
    public Boolean checkColumnEncoded(Long siteId, Long parentId, Long columnId, String columnEncoded) {

        // 检验格式，是小写字母和下划线组成的才符合要求
        if (!ValidateUtil.LetterINumber(columnEncoded)) return false;

        // 同级栏目集合
        List<ColumnPO> columnPOList = null;

        // 顶级栏目
        if (parentId.equals(0L)) {
            // 根据站点id到中间表中查询这个站点下所有的顶级栏目
            QueryWrapper<SiteColumnPO> siteColumnWrapper = new QueryWrapper<>();
            siteColumnWrapper.eq("SITE_ID", siteId);

            ArrayList<Long> queryInColumnIdList = new ArrayList<>();
            List<SiteColumnPO> siteColumnPOList = siteColumnMapper.selectList(siteColumnWrapper);

            // 站点下没有栏目的情况
            if (siteColumnPOList == null || siteColumnPOList.size() == 0) return true;

            for (SiteColumnPO siteColumnPO : siteColumnPOList) {
                queryInColumnIdList.add(siteColumnPO.getColumnId());
            }

            QueryWrapper<ColumnPO> columnPOQueryWrapper = new QueryWrapper<>();
            columnPOQueryWrapper.in("ID", queryInColumnIdList);
            // 更新的情况下，排除了自己，新增的情况下忽视
            if (columnId != null) {
                columnPOQueryWrapper.ne("ID", columnId);
            }
            columnPOQueryWrapper.eq("IS_DELETED", 0);
            columnPOList = columnMapper.selectList(columnPOQueryWrapper);
            if (columnPOList == null || columnPOList.size() == 0) {
                return true;
            }
        } else {
            // 非顶级栏目
            QueryWrapper<ColumnPO> columnPOQueryWrapper = new QueryWrapper<>();
            columnPOQueryWrapper.eq("PARENT_ID", parentId);
            // 更新
            if (columnId != null) {
                columnPOQueryWrapper.ne("ID", columnId);
            }
            columnPOQueryWrapper.eq("IS_DELETED", 0);
            columnPOList = columnMapper.selectList(columnPOQueryWrapper);
            // 没有同级栏目，就是新增，直接返回true
            if (columnPOList == null || columnPOList.size() == 0) {
                return true;
            }
        }
        // 上面的代码得到了除了自己的同级栏目

        for (ColumnPO columnPO : columnPOList) {
            if (columnPO.getEncoded().equals(columnEncoded)) {
                return false;
            }
        }
    return true;
    }

    /**
     * 获取静态文件存储路径
     * @return
     */
    @Override
    public String getStorageStaticPath(Long columnId) {
        // 1. 查询栏目模板     --编码？
        String columnStaticPath = "";
        ColumnPO columnPO = columnMapper.selectById(columnId);

        // 在调用此方法的位置已经检查过columnPO不为空

        // 2. 拼接父级栏目模板     --编码？

        // 如果不是顶级栏目，就通过xPath获取祖先级栏目的id
        if (!columnPO.getParentId().equals(0L)){
            String[] columnSplit = columnPO.getXpath().split("-");
            // 遍历祖先级栏目的id集合，并获取栏目的编码
            for (int j = 1;j < columnSplit.length; j++) {
                ColumnPO columnParentPO = columnMapper.selectById(columnSplit[j]);
                // 判断是否查询到，查到就拼接
                if (!ValidateUtil.isNullOrEmpty(columnParentPO)) {
                    columnStaticPath = columnStaticPath + File.separator + columnParentPO.getEncoded();
                }
            }
        }

        // 拼上本栏目的编码
        columnStaticPath = columnStaticPath + File.separator + columnPO.getEncoded();

        // 3. 拼上 index.html------>放到调用的地方去拼
//        columnStaticPath = columnStaticPath + ".html";

        return columnStaticPath;
    }

}
